﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BubbleBlastSolver.Classes.BusinessLogic;
using System.Diagnostics;

namespace BubbleBlastSolver
{
    public partial class frmBubbleSessionEntry : Form
    {
        private PictureBox[,] m_imageArray = new PictureBox[BubbleSession.ROW_COUNT, BubbleSession.COLUMN_COUNT];

        public frmBubbleSessionEntry()
        {
            InitializeComponent();

            this.CreateTableImages();

            this.bubblesTable.Refresh();

            this.radioButton1.Tag = 1;
            this.radioButton2.Tag = 2;
            this.radioButton3.Tag = 3;
            this.radioButton4.Tag = 4;
        }

        private void CreateTableImages()
        {
            PictureBox pictureBox;
            for (uint row = 0; row < BubbleSession.ROW_COUNT; ++row)
            {
                for (uint column = 0; column < BubbleSession.COLUMN_COUNT; ++column)
                {
                    pictureBox =  this.CreateBubbleImage(row, column, (uint)0);
                    this.SetPictureBoxInArray(row, column, pictureBox);
                    this.bubblesTable.Controls.Add(this.GetPictureBoxFromArray(row, column), (int)column, (int)row);
                }
            }
        }

        private PictureBox GetPictureBoxFromArray(uint row, uint col)
        {
            return this.m_imageArray[row, col];
        }

        private void SetPictureBoxInArray(uint row, uint col, PictureBox pictureBox)
        {
            this.m_imageArray[row, col] = pictureBox;
        }
        
        private PictureBox CreateBubbleImage(uint row, uint column, uint weight)
        {
            PictureBox pictureBox = new PictureBox();

            switch(weight)
            {
                case 0:
                pictureBox.Image = global::BubbleBlastSolver.Properties.Resources.White;
                pictureBox.Tag = 0;
                break;

                case 1:
                pictureBox.Image = global::BubbleBlastSolver.Properties.Resources.Red;
                pictureBox.Tag = 1;
                break;

                case 2:
                pictureBox.Image = global::BubbleBlastSolver.Properties.Resources.Green;
                pictureBox.Tag = 2;
                break;

                case 3:
                pictureBox.Image = global::BubbleBlastSolver.Properties.Resources.Yellow;
                pictureBox.Tag = 3;
                break;

                case 4:
                pictureBox.Image = global::BubbleBlastSolver.Properties.Resources.Blue;
                pictureBox.Tag = 4;
                break;

                default:
                    throw new Exception("Wrong weight!");
            }

            pictureBox.Location = new System.Drawing.Point(0, 0);
            pictureBox.Name = "pictureBox" + row.ToString() + column.ToString();
            pictureBox.Size = new System.Drawing.Size(50, 50);
            pictureBox.TabIndex = 0;
            pictureBox.TabStop = false;
            pictureBox.Click += new System.EventHandler(this.pictureBox_Click);

            return pictureBox;
        }

        public uint ClickCount
        {
            get
            {
                return (uint)this.numClickCount.Value;
            }
        }

        public uint GetBubbleWeight(uint row, uint column)
        {
            PictureBox pb = this.GetPictureBoxFromArray(row, column);

            return Convert.ToUInt32(pb.Tag);
        }

        private void pictureBox_Click(object sender, EventArgs e)
        {
            PictureBox pictureBox = (PictureBox)sender;

            pictureBox.Tag = this.GetSelectedImage();

            if ((uint)pictureBox.Tag == 0)
            {
                pictureBox.Image = global::BubbleBlastSolver.Properties.Resources.White;
            }
            else if ((uint)pictureBox.Tag == 1)
            {
                pictureBox.Image = global::BubbleBlastSolver.Properties.Resources.Red;
            }
            else if ((uint)pictureBox.Tag == 2)
            {
                pictureBox.Image = global::BubbleBlastSolver.Properties.Resources.Green;
            }
            else if ((uint)pictureBox.Tag == 3)
            {
                pictureBox.Image = global::BubbleBlastSolver.Properties.Resources.Yellow;
            }
            else
            {
                pictureBox.Image = global::BubbleBlastSolver.Properties.Resources.Blue;
            }
        }

        private uint GetSelectedImage()
        {
            if (this.radioButton0.Checked)
            {
                return 0;
            }
            else if (this.radioButton1.Checked)
            {
                return 1;
            }
            else if (this.radioButton2.Checked)
            { 
                return 2;
            }
            else if (this.radioButton3.Checked)
            {
                return 3;
            }
            else
            {
                return 4;
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            //this.DialogResult = System.Windows.Forms.DialogResult.OK;
            //this.Close();

            BubbleSession startSession = new BubbleSession();
            uint imageBubbleWeight = 0;

            for (uint row = 0; row < BubbleSession.ROW_COUNT; ++row)
            {
                for (uint col = 0; col < BubbleSession.COLUMN_COUNT; ++col)
                {
                    imageBubbleWeight = this.GetBubbleWeight(row, col);

                    if (imageBubbleWeight > 0)
                    {
                        startSession.CreateBubbleIn(row, col, imageBubbleWeight);
                    }
                }
            }

            startSession.Initialize();
            startSession.Index = 1;

            Game game = new Game();
            game.Solve(startSession, this.ClickCount);

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnTest_Click(object sender, EventArgs e)
        {
            //Game game = new Game();
            //game.Test();

            int[] digits = new int[] { 0, 0, 0, 0 };

            for (int d = 0; d < 4; ++d)
            {
                for (int c = 0; c < 4 ; ++c)
                {
                    for (int b = 0; b < 4; ++b)
                    {
                        for (int a = 0; a < 4; ++a)
                        {
                            digits[3] = d;
                            digits[2] = c;
                            digits[1] = b;
                            digits[0] = a;

                            if (AreDigitsUnique(digits))
                            {
                                Debug.WriteLine(String.Format("{0}{1}{2}{3}", digits[0], digits[1], digits[2], digits[3]));
                            }
                        }
                    }
                }
            }
        }

        private bool AreDigitsUnique(int[] digits)
        {
            int[] digitsCount = new int[] { 0, 0, 0, 0 };

            for (int i = 0; i < 4; ++i)
            {
                digitsCount[ digits[i] ] += 1;
            }

            for (int i = 0; i < 4;  ++i)
            {
                if (digitsCount[i] != 1)
                {
                    return false;
                }
            }

            return true;
        }

        private void radioButton_CheckedChanged(object sender, EventArgs e)
        {           

        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            for (uint row = 0; row < BubbleSession.ROW_COUNT; ++row)
            {
                for (uint col = 0; col < BubbleSession.COLUMN_COUNT; ++col)
                {
                    PictureBox pb = this.GetPictureBoxFromArray(row, col);
                    pb.Image = global::BubbleBlastSolver.Properties.Resources.White;
                    pb.Tag = 0;
                }
            }
        }
    }
}
